package br.com.werp.suporte.Geral;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import com.egen.util.jdbc.*;
import com.egen.util.text.*;

/**
 * Creation Date 02/05/2005 10:34:59
 * Last Modify Date 08/02/2007 09:09:21
 */

public class VerificaCepAction extends com.egen.util.struts.AbstractAction {

  public ActionForward perform_pesquisar_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    try {

      //#####################################################################################

      VerificaCepActionForm f = (VerificaCepActionForm) form;
      br.com.werp.regras.geral.GerenciamentoEndereco gerenciamentoEndereco =
        br.com.werp.regras.geral.FabricaSubSistema.getGerenciamentoEndereco();
      session.setAttribute("ceps",gerenciamentoEndereco.pesquisarCep(f));

      //#####################################################################################

      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    return actionForward;
  }

  public ActionForward perform_resetfull_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    try {
      //---------------------------------------------------------
      form.reset(null, null);
      session.setAttribute("ceps",new ArrayList());
      //---------------------------------------------------------
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    return actionForward;
  }

  public ActionForward perform_verifica_ajax_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    try {
      //----------------------------------------------------------------------
      VerificaCepActionForm f = (VerificaCepActionForm) form;
      br.com.werp.regras.geral.GerenciamentoEndereco gerenciamentoEndereco =
        br.com.werp.regras.geral.FabricaSubSistema.getGerenciamentoEndereco();
      request.setAttribute("form_ajax_respose",
          gerenciamentoEndereco.pesquisarCep(f.getCeps_numero()));
      actionForward = mapping.findForward("ajax");
      //----------------------------------------------------------------------
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    return actionForward;
  }

  public ActionForward perform_carregar_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    try {
      //---------------------------------------------------------
      String usur_login = request.getParameter("usur_login");
      if (usur_login != null || !"".equals(usur_login)) {
        session.setAttribute("j_username",usur_login);
      }
      session.setAttribute("ceps",new ArrayList());
      session.setAttribute("cep",request.getParameter("cep"));
      session.setAttribute("cepLabel",request.getParameter("cepLabel"));
      session.setAttribute("logradouro",request.getParameter("logradouro"));
      session.setAttribute("bairro",request.getParameter("bairro"));
      session.setAttribute("cidade",request.getParameter("cidade"));
      session.setAttribute("estado",request.getParameter("estado"));
      //---------------------------------------------------------
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    return actionForward;
  }

  public ActionForward perform_popup_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    try {
      //-------------------------------------------------------------------------------
      session.setAttribute("ceps",new ArrayList());
      session.setAttribute("cep",request.getParameter("cep"));
      session.setAttribute("cepLabel",request.getParameter("cepLabel"));
      session.setAttribute("logradouro",request.getParameter("logradouro"));
      session.setAttribute("bairro",request.getParameter("bairro"));
      session.setAttribute("cidade",request.getParameter("cidade"));
      session.setAttribute("estado",request.getParameter("estado"));

      //-------------------------------------------------------------------------------

      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    return actionForward;
  }

}
